package com.example.supplychainbackend.entity.purchase;

import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.persistence.*;
import lombok.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Entity
@Table(name = "purchase_order")
@Data
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PurchaseOrderEntity {
    @Id
    private String purchaseOrderNumber;

    private Integer status;
    private String salesOrderName;
    private String contractNumber;
    private String contractName;
    private String supplierCode;
    private String supplierName;
    private Float totalAmountWithTax;
    private String plannedEarliestDeliveryDate;
    private String promisedDeliveryDate;
    private String supplierRemarks;
    private String headerRemarks;
    private String lastUpdateDate;
    private String supplierOrderNumber;
    private Integer orderType;
    private Float payableQuantity;
    private String purchaser;
    private String orderCreator;
    private String orderDate;
    private Integer purchaseType;
    private String recentApprovalTime;
    private Integer paymentMethod;
    private String localCurrency;
    private String paymentCycle;
    private String originalCurrency;
    private Float exchangeRate;
    private String tradeTerms;
    private String destination;
    private String email;
    private String shippingAddress;
    private String detailAddress;
    private String companyCodeName;
    private String factoryName;
    private String paymentNotes;

    private String salesOrderNumber;
    private String plannedDeliveryDate;
    private String contractType;
    //在PurchaseMaterial中创建purchase_order_number来指向PurchaseOrder
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "purchase_order_number")
    private List<PurchaseMaterialEntity> materialList;

    @Transient
    private static final AtomicInteger counter = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (purchaseOrderNumber == null || purchaseOrderNumber.equals("")) {
            purchaseOrderNumber = String.format("%s%s%04d",
                    "PRO", DateTimeUtils.getCurrentDateForID(), counter.getAndIncrement());
        }
    }

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;
}
